package com.xiao.sulingother.controller.oss;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author: 1322127758@qq.com
 * @date: 2022/3/18 20:34
 * @作用:
 */
// 表示返回的数据以json的方式; 功能来源于这个注解@ResponseBody
@RestController
@RequestMapping("/other/oss")
public class OssController {

    // 请填写您的AccessKeyId。
    @Value("${mine.mineAccessId}")
    private String accessId;
    // 请填写您的AccessKeySecret。
    @Value("${mine.mineAccessKey}")
    private String accessKey;
    // 请填写您的 endpoint。
    @Value("${mine.mineEndpoint}")
    private String endpoint;
    // 请填写您的 bucketname 。
    @Value("${mine.mineBucket}")
    private String bucket;
    // host的格式为 bucketname.endpoint
    private String host;
    // 用户上传文件时指定的前缀。就是上传到哪个目录
    private String dir;

    @RequestMapping("/party/sign")
    public Map<String, String> getOssSign(){
        host = "https://" + bucket + "." + endpoint;

        //创建一个时间;使用这个时间当文件夹
        String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        dir = format + "/";

        // 创建OSSClient实例。
        Map<String, String> respMap = null;
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessId, accessKey);
        try {
            long expireTime = 30;
            long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
            Date expiration = new Date(expireEndTime);
            // PostObject请求最大可支持的文件大小为5 GB，即CONTENT_LENGTH_RANGE为5*1024*1024*1024。
            PolicyConditions policyConds = new PolicyConditions();
            policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
            policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);

            String postPolicy = ossClient.generatePostPolicy(expiration, policyConds);
            byte[] binaryData = postPolicy.getBytes("utf-8");
            String encodedPolicy = BinaryUtil.toBase64String(binaryData);
            String postSignature = ossClient.calculatePostSignature(postPolicy);

            respMap = new LinkedHashMap<String, String>();
            respMap.put("accessid", accessId);
            respMap.put("policy", encodedPolicy);
            respMap.put("signature", postSignature);
            respMap.put("dir", dir);
            respMap.put("host", host);
            respMap.put("expire", String.valueOf(expireEndTime / 1000));


        } catch (Exception e) {
            // Assert.fail(e.getMessage());
            System.out.println(e.getMessage());
        } finally {
            ossClient.shutdown();
        }

        return respMap;
    }

}